package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Destination;
import cn.wolfcode.domain.Region;
import cn.wolfcode.query.DestinationQuery;
import cn.wolfcode.repository.DestinationRepository;
import cn.wolfcode.service.IDestinationService;
import cn.wolfcode.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class DestinationServiceImpl implements IDestinationService {

    @Autowired
    private DestinationRepository destinationRepository;
    @Autowired
    private RegionServiceImpl regionService;
    @Autowired
    private MongoTemplate template;

    @Override
    public void save(Destination destination) {
        destination.setId(null);
        destinationRepository.save(destination);
    }

    @Override
    public void update(Destination destination) {
        destinationRepository.save(destination);
    }

    @Override
    public void delete(String id) {
        destinationRepository.deleteById(id);
    }

    @Override
    public Destination get(String id) {
        return destinationRepository.findById(id).orElse(null);

    }

    @Override
    public List<Destination> findAll() {
        return destinationRepository.findAll();
    }

    @Override
    public List<Destination> queryDestByRegionId(String rid) {
        Region region = regionService.get(rid);
        List<String> ids = region.getRefIds();

        return destinationRepository.findByIdIn(ids);
    }

    @Override
    public void updateIsHotById(int hot, String id) {
        if(StringUtils.hasLength(id)){
            Region region = regionService.get(id);
            region.setIshot(hot);
            regionService.update(region);
        }
    }


    public Page<Destination> query(DestinationQuery qo) {
        Query query = new Query();


        //分页参数对象, 参数1 当前页  2 每页显示的条数  3 排序规则  4 排序字段
        Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.ASC, "_id");

        if(StringUtils.hasLength(qo.getKeyword())){
            Criteria name = Criteria.where("name").regex(qo.getKeyword());
            query.addCriteria(name);
        }

        if(StringUtils.hasLength(qo.getParentId())){
            query.addCriteria(Criteria.where("parentId").is(qo.getParentId()));
        }else{
            query.addCriteria(Criteria.where("parentId").is(null));
        }

        query.with(pageable);

        List<Region> list = template.find(query, Region.class);

        return DBHelper.query(template, Destination.class,query,pageable);
    }

    @Override
    public List<Destination> queryToasts(String parentId) {
        List<Destination> list = new ArrayList<>();
        this.createToasts(list, parentId);
        //倒序排列
        Collections.reverse(list);
        return list;
    }

    @Override
    public List<Destination> queryDestByRegionIdForApi(String regionId) {
        List<Destination> list = new ArrayList<>();
        //区域,国内
        if("-1".equals(regionId)){
            list = destinationRepository.findByParentName("中国");
        }else{
            list = this.queryDestByRegionId(regionId);
        }

        //第二层
        for (Destination dest : list) {

            Pageable pageable = PageRequest.of(0, 5, Sort.Direction.ASC, "_id");

            List<Destination> children = destinationRepository.findByParentId(dest.getId(),pageable);
            dest.setChildren(children);
        }
        return list;
    }

    @Override
    public Destination getByName(String name) {


        return destinationRepository.findByName(name);
    }

    private void createToasts(List<Destination> list, String parentId){
        if(!StringUtils.hasLength(parentId)){
            return;
        }
        Destination destination = this.get(parentId);
        list.add(destination);
        if(StringUtils.hasLength(destination.getParentId())){
            createToasts(list, destination.getParentId());
        }

    }
}
